/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package de.swta;

import de.swta.components.category.CategoryManager;
import de.swta.components.unit.UnitManager;
import java.util.Arrays;
import java.util.List;
import org.apache.wicket.Component;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxSubmitLink;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.RequiredTextField;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.validation.validator.RangeValidator;
import org.apache.wicket.validation.validator.StringValidator.LengthBetweenValidator;

/**
 *
 * @author JKorreck
 */
public class ItemForm extends Form {

    private RequiredTextField nameField = new RequiredTextField("name");
    final FeedbackPanel feedbackPanel = new FeedbackPanel("feedback");
    private RequiredTextField idField = new RequiredTextField("id");
    private RequiredTextField weightField = new RequiredTextField("weight");
    private RequiredTextField priceField = new RequiredTextField("price");
    private DropDownChoice unitDropDown;
    private DropDownChoice categoryDropDown;
    Component parent;

    public ItemForm(String id, Component parent) {
        super(id);

        this.parent = parent;
        MyItem item = new MyItem();
        this.setModel(new CompoundPropertyModel(item));
        feedbackPanel.setOutputMarkupId(true);

        this.unitDropDown = new DropDownChoice("unit", UnitManager.getInstance().getUnitNames());
        this.categoryDropDown = new DropDownChoice("category", CategoryManager.getInstance().getCategoryNames());
        this.add(idField.add(new LengthBetweenValidator(3, 20)));
        this.add(categoryDropDown);
        this.add(nameField.add(new LengthBetweenValidator(3, 20)));
        this.add(weightField.add(new RangeValidator(100, 2000)));
        this.add(priceField.add(new RangeValidator(1d, 2000d)));
        this.add(unitDropDown);

        this.add(new AjaxSubmitLink("saveLink") {

            @Override
            protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
                ItemForm.this.onSubmit(target);
                System.out.println("ItemForm.this.parent: " + ItemForm.this.parent.getClassRelativePath());
                target.addComponent(ItemForm.this.parent);
            }

            @Override
            protected void onError(AjaxRequestTarget target, Form<?> form) {
                target.addComponent(ItemForm.this.feedbackPanel);
                super.onError(target, form);
            }
        });
        this.add(feedbackPanel);
    }

    private void onSubmit(AjaxRequestTarget target) {

        MyItem itemModel = (MyItem) getModelObject();
        this.feedbackPanel.info("Item was saved!");
        MyItemManager.getInstance().addItem(itemModel);
    }
}